<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\log;

/**
 * The <b>LoggerFactory</b> class is the base class for all logger producers. Logger
 * factories are instanciated and used by the <i>Switcher</i> framework to create loggers
 * injected into objects managed by the framework.
 * 
 * <p>The logger factory is declared in the configuration part of the framework. By default,
 * if no logger factory is declared, a <b>SilentLogger</b> is instanciated. The logger
 * factory produces loggers depending on the targets classes.</p>
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @package switcher\log
 * @since 1.0.0
 * @see switcher\config\Config
 * @see SilentLogger
 * @see SystemLogger
 */
abstract class LoggerFactory
{
	/**
	 * Shared logger factory
	 * @var LoggerFactory
	 */
	private static $loggerFactory;
	
	/**
	 * No-arg constructor.
	 */
	public function __construct()
	{
	}
	
	/**
	 * Returns a <b>Logger</b> for the specified class.
	 * 
	 * @param $class The class of the object to enable logging for
	 * @return Logger A logger for the specified class
	 */
	public abstract function getLogger($class);
	
	/**
	 * Define the logger factory used by the framework.
	 * 
	 * @param LoggerFactory $loggerFactory The logger factory to use
	 * @throws \InvalidArgumentException If $loggerFactory is not a logger factory
	 */
	public static function setLoggerFactory($loggerFactory)
	{
		self::checkLoggerFactory($loggerFactory);
		self::$loggerFactory = $loggerFactory;
	}
	
	/**
	 * Injects a logger into an object with the logger factory.
	 * 
	 * @param mixed $object The object to inject a logger into.
	 * @throws \InvalidArgumentException If the object is not eligible for logging
	 * @throws \InvalidArgumentException If $loggerFactory is not a logger factory
	 * @throws \InvalidArgumentException If the logger created is not a logger
	 * @see switcher\log\Logger
	 * @see switcher\log\LoggerFactory
	 */
	public static function injectLogger($object)
	{
		if ( !is_object($object) )
		{
			throw new \InvalidArgumentException('Not an object : ' . (string) $object);
		}
		
		self::initializeLoggerFactory();
		self::checkLoggerFactory(self::$loggerFactory);
		
		try
		{
			$logger = self::$loggerFactory->getLogger( get_class($object) );
			self::checkLogger($logger);
			\switcher\utils\ReflectionHelper::setProperty($object, 'logger', $logger);
		}
		catch (\ReflectionException $e)
		{
			throw new \InvalidArgumentException('Cannot inject logger : ' . get_class($object), $e->getCode(), $e);
		}
	}
	
	/**
	 * Creates a SilentLogger if needed.
	 * 
	 * @see switcher\log\SilentLogger
	 */
	private static function initializeLoggerFactory()
	{
		if (self::$loggerFactory == null)
		{
			self::$loggerFactory = new SilentLogger();
		}
	}
	
	/**
	 * Ensures that $loggerFactory is a logger factory.
	 * 
	 * @throws \InvalidArgumentException If $loggerFactory is not a logger factory
	 */
	private static function checkLoggerFactory($loggerFactory)
	{
		if ( !is_object($loggerFactory) )
		{
			throw new \InvalidArgumentException('Not a Logger Factory : ' . $loggerFactory);
		}
		
		if ( !is_subclass_of($loggerFactory, '\switcher\log\LoggerFactory') )
		{
			throw new \InvalidArgumentException('Not a Logger Factory : ' . get_class($loggerFactory));
		}
	}
	
	/**
	 * Ensures that $logger is a logger.
	 * 
	 * @throws \InvalidArgumentException If $logger is not a logger
	 * @see switcher\log\Logger
	 */
	private static function checkLogger($logger)
	{
		if ( !is_object($logger) )
		{
			throw new \InvalidArgumentException('Not a Logger : ' . $logger);
		}
		
		if ( !is_a($logger, '\switcher\log\Logger') )
		{
			throw new \InvalidArgumentException('Not a Logger : ' . get_class($logger));
		}
	}
}

?>